package org.metasketch.demo.diagrams.ATD;

import javafx.geometry.Orientation;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.ToolBar;

import org.metasketch.demo.model.ActorRole;
import org.metasketch.demo.model.OrganizationalUnit;
import org.metasketch.demo.model.TransactionKind;
import org.metasketch.diagram.javafx.graphics.DefaultLink;
import org.metasketch.editor.links.GraphicalEndConfig;
import org.metasketch.editor.links.GraphicalLinkConfig;
import org.metasketch.editor.links.LinkEndOrientation;
import org.metasketch.editor.links.LinkEndType;
import org.metasketch.editor.links.SemanticConfig;
import org.metasketch.editor.toolbox.DefaultDiagramBuilder;
import org.metasketch.editor.toolbox.DefaultSelectHandler;
import org.metasketch.editor.toolbox.SimpleRelation;
import org.metasketch.editor.toolbox.SimpleToolBaseDiagramType;

public class ATDDiagram extends SimpleToolBaseDiagramType {

	public ATDDiagram() {
		super("Actor Transaction Diagram", new DefaultDiagramBuilder());

		registerTool("Select", null, "Select", new DefaultSelectHandler(), null);

		registerToolClasses("Organization Unit", null,
				"OrganizationUnit-ATD-Node", OrganizationalUnit.class,
				OrganizationUnitNode.class, true);
		registerToolClasses("Actor Role", null, "ActorRole-ATD-Node",
				ActorRole.class, ActorRoleNode.class, true);
		registerToolClasses("Transaction Kind", null,
				"TransactionKind-ATD-Node", TransactionKind.class,
				TransactionKindNode.class, true);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("OrganizationUnit-ATD-Node");
		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("ActorRole-ATD-Node");
		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("TransactionKind-ATD-Node");

		// semantic config, source EndType, target EndType
		registerDefaultGraphicalLink("empty link", "empty-link", null);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("empty-link");

		// SemanticConfig cfg = new SemanticConfig();

		// EndConfig source = new EndConfig();
		// source.getTypes().add("TransactionKind-ATD-Node");
		//
		// EndConfig target = new EndConfig();
		// target.getTypes().add("ActorRole-ATD-Node");

		GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();
		linkConfigA.setSourceTypes("ActorRole-ATD-Node");
		linkConfigA.setSourceFieldName("initiates");
		linkConfigA.getSource().getLinkEnd()
				.setGraphic(LinkEndType.FILLEDSQUARE);
		// linkConfigA.setSourceSemanticFieldName("name");
		linkConfigA.getSource().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);

		linkConfigA.setTargetTypes("TransactionKind-ATD-Node");
		linkConfigA.setTargetFieldName("initiatedBy");
		// linkConfigA.getTarget().getLinkEnd().setGraphic(LinkEndType.SQUARE);
		// linkConfigA.setTargetSemanticFieldName("name");
		linkConfigA.getTarget().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);
		// import org.metasketch.demo.model.*;
		String initiateScript = "targetSemantic.setInitiateActorRole(sourceSemantic);";
		linkConfigA.setScript(initiateScript);

		registerDefaultGraphicalLink("initiate link", "initiate-link", null,
				linkConfigA);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("initiate-link");

		GraphicalLinkConfig linkConfigB = new GraphicalLinkConfig();
		linkConfigB.setSourceTypes("TransactionKind-ATD-Node");
		linkConfigB.setSourceFieldName("executedBy");
		linkConfigB.getSource().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);
		linkConfigB.setTargetTypes("ActorRole-ATD-Node");
		linkConfigB.setTargetFieldName("executes");
		linkConfigB.getTarget().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);

		String executeScript = "sourceSemantic.setExecuteActorRole(targetSemantic);";
		linkConfigB.setScript(executeScript);

		registerDefaultGraphicalLink("execute link", "execute-link", null,
				linkConfigB);
		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("execute-link");
		// registerDefaultLink("test link", "test-link", null, linkConfigA,
		// linkConfigB);

		// registerLink("test link", "test-link", ConfigurableLink.class,null,
		// linkConfigA,
		// linkConfigB);

		// test
		GraphicalLinkConfig linkConfigC = new GraphicalLinkConfig();
		linkConfigC.setSourceTypes("ActorRole-ATD-Node");
		// linkConfigC.setSourceFieldName("initiates");
		linkConfigC.getSource().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);
		linkConfigC.setTargetTypes("ActorRole-ATD-Node");
		// linkConfigC.setTargetFieldName("initiatedBy");
		linkConfigC.getTarget().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);

		registerDefaultGraphicalLink("actor link", "actor-link", null,
				linkConfigC);

		// adding child/parent relations
		relationManager.register(new SimpleRelation(ActorRole.class,
				OrganizationalUnit.class, "assignedTo", "assignedActorRoles"));
		relationManager.register(new SimpleRelation(OrganizationalUnit.class,
				OrganizationalUnit.class, null, "containedOrganizations"));

	}

	@Override
	public ToolBar createToolbar() {

		ToolBar toolBar = new ToolBar(new Label("Entities"),
				getTypeButton("Select"),
				getTypeButton("TransactionKind-ATD-Node"),
				getTypeButton("ActorRole-ATD-Node"),
				getTypeButton("OrganizationUnit-ATD-Node"), new Separator(),
				new Label("Relations"),

				getTypeButton("empty-link"),

				getTypeButton("initiate-link"), getTypeButton("execute-link")

		// ,getTypeButton("actor-link"),
		//
		// createButton("Initiates"), createButton("Executes"),
		// createButton("test1"), new Separator(), new Label("Test"),
		// createButton("test2"), createButton("test3")

		);

		toolBar.setOrientation(Orientation.VERTICAL);

		// toolBar.set
		// TODO setup action handling

		return toolBar;
	}

}
